/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HTML_TO_JSP.java
 *
 * Created on Nov 14, 2013, 12:13:22 PM
 */
package htmltojsp;

import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JOptionPane;

/**
 *
 * @author imal
 */
public class HTML_TO_JSP extends javax.swing.JFrame {

    /** Creates new form HTML_TO_JSP */
    public HTML_TO_JSP() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        sourcefolder = new javax.swing.JTextField();
        progressofconverstion = new javax.swing.JProgressBar();
        startconverstion = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        numbfils = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        allfiles = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Source Path");

        startconverstion.setText("Start Converstion");
        startconverstion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startconverstionActionPerformed(evt);
            }
        });

        jLabel3.setText("files :");

        numbfils.setText(" ");

        allfiles.setColumns(20);
        allfiles.setEditable(false);
        allfiles.setRows(5);
        jScrollPane2.setViewportView(allfiles);

        jButton1.setText("Settings");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(startconverstion, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(numbfils, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(165, 165, 165))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sourcefolder, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))
                            .addComponent(progressofconverstion, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(sourcefolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(startconverstion, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(numbfils))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(progressofconverstion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-416)/2, (screenSize.height-353)/2, 416, 353);
    }// </editor-fold>//GEN-END:initComponents

    private void startconverstionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startconverstionActionPerformed
        if(!sourcefolder.getText().trim().isEmpty()){
        
        ConversionLogic htjsp = new ConversionLogic(sourcefolder.getText());
        allfiles.setText(htjsp.fetchFiles());
        numbfils.setText(ConversionLogic.TOTAL_FILES + "");
        htjsp.convertALL();
        progressofconverstion.setMaximum(ConversionLogic.TOTAL_FILES);
        Thread tq2 = new Thread(new Runnable() {

            @Override
            public void run() {
                final Timer uploadCheckerTimer = new Timer(true);

                uploadCheckerTimer.scheduleAtFixedRate(
                        new TimerTask() {
                            public void run() {
                                progressofconverstion.setValue(ConversionLogic.CURRENT_READ_FILE);
                                if(ConversionLogic.CURRENT_READ_FILE == ConversionLogic.TOTAL_FILES){
                                    JOptionPane.showMessageDialog(null, "Coversion Completed! ", "SUCCESS", JOptionPane.INFORMATION_MESSAGE);
                                    uploadCheckerTimer.cancel();
                                }
                            }
                        }, 0, 10);
            }
        });
        
        tq2.start();
    }else{
        JOptionPane.showMessageDialog(null, "You need to have a valid Source Path", "ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_startconverstionActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       new settings().setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new HTML_TO_JSP().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea allfiles;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel numbfils;
    private javax.swing.JProgressBar progressofconverstion;
    private javax.swing.JTextField sourcefolder;
    private javax.swing.JButton startconverstion;
    // End of variables declaration//GEN-END:variables
}
